package com.gang.ssm.bos.system.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gang.ssm.bos.core.anotation.Permission;
import com.gang.ssm.bos.core.toolbox.ajax.AjaxResult;
/**
 * 流程定义
 * @author Administrator
 *
 */
@RequestMapping("/processDefinition")
@Controller
public class ProcessDefinitionController extends BosController {
	
	@Autowired
	private RepositoryService repositoryService;

	@RequestMapping("/list")
	public String list(Map<String, Object> map) {
		ProcessDefinitionQuery query = repositoryService
				.createProcessDefinitionQuery();
		query.latestVersion();
		query.orderByProcessDefinitionName().desc();
		
		List<ProcessDefinition> list = query.list();
		map.put("list", list);
		return "system/workflow/processdefinition_list";
	}
	
	@RequestMapping("/showPng/{id}")
	public String showpng(@PathVariable("id") String id,HttpServletResponse response) throws IOException {
		InputStream inputStream = repositoryService.getProcessDiagram(id);
		byte[] fileDate = FileCopyUtils.copyToByteArray(inputStream);
		response.setContentType("image/png");
		/*response.addHeader("Content-Disposition", "attachment;filename=image.png");加上这句会显示下载的提示*/
		OutputStream out = response.getOutputStream();
		out.write(fileDate);
		return null;
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxResult delete(@PathVariable("id")String id, HttpServletResponse response) throws UnsupportedEncodingException {
		ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
		query.processDefinitionId(id);
		ProcessDefinition processDefinition = query.singleResult();
		String deploymentId = processDefinition.getDeploymentId();
		try {
			repositoryService.deleteDeployment(deploymentId);
			return success("删除成功!");
		} catch (Exception e) {
		}
		
		return fail("删除失败!");
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/deploy")
	public AjaxResult deploy(@RequestParam("zipFile") MultipartFile file) throws IOException {
		try {
			if(!file.isEmpty()) {
				DeploymentBuilder deploymentBuilder =  repositoryService.createDeployment();
				deploymentBuilder.addZipInputStream(new ZipInputStream(file.getInputStream()));
				deploymentBuilder.deploy();
				
				return success("导入成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("导入失败!");
	}
}
